<?php 
/**
 * $Id: hooks.php 206 2012-05-22 11:08:08Z shrey $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * This is parent of parent class of a controller and should not be edited ever without permission.
 * @version 			$Rev: 206 $
 * @lastModifiedBy		$LastChangedBy: shrey $
 * @lastChanged			$Date: 2012-05-22 16:38:08 +0530 (Tue, 22 May 2012) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

/**
 * beforeFilter hook to do various operations
 *
 * @param unknown_type $controller
 * @return unknown
 */

function BackendBeforeFilterHook($controller)
{
	// if its the administrator/manager - change the layout
	if(isset($controller->params['prefix']) && $controller->params['prefix'] == 'admin') 
	{
		$controller->helpers[] = 'Backend.Backend';
		
		$controller->layout = Configure::read('Backend.adminLayout');

		if(Configure::read('cmsConfig.Theme.adminTheme'))
		{
			$controller->layout = "admin_".Configure::read('cmsConfig.Theme.adminTheme');
			//$controller->theme = Configure::read('cmsConfig.Theme.adminTheme');
		}
		
		// tell Auth not to interfere with admin functions
		if(isset($controller->Auth) && is_object($controller->Auth))
		{
			//fix for admin_camleCased methods
			$controller->Auth->allow($controller->action);
		}
		
		// check if login function executing
		if($controller->name=='Backend' &&  $controller->action=='admin_login')
		{
			return true;
		}
		// check if this is a first run
		if($controller->name=='Config' &&  $controller->action=='firstRun')
		{
			return true;
		}
		if(!$controller->_isAdmin())
		{
			$controller->errorMsg(__("No access."));
			$controller->redirect(Configure::read('Backend.adminLoginAction'));
		}
		else
		{
			Configure::write('adminLoggedIn', true);
		}
	}
}

/**
 * beforeRender hook to make sure admin errors displays with admin layout only
 *
 * @param unknown_type $controller
 */
function BackendbeforeRenderHook(&$controller)
{
	// hack for displaying backend errors
	if($controller->name=='CakeError' && isset($controller->params['admin']))
	{
		$controller->layout = Configure::read('Backend.adminLayout');
		if(Configure::read('cmsConfig.Theme.adminTheme'))
		{
			$controller->layout = "admin_".Configure::read('cmsConfig.Theme.adminTheme');
		}
	}
}

// @todo: Find a way to enable admin while keeping rest of site offline
function BackendBeforeRoutesInitHook()
{
	if(Configure::read('cmsConfig.Backend.siteStatus') === 'off')
	{
		Router::connect('/', array('plugin'=>'cms', 'controller' => 'cms', 'action' => 'offline'));
		//Router::connect('/*', array('plugin'=>'cms', 'controller' => 'cms', 'action' => 'offline'));
	}
}
function BackendSetIntallerStepsHook($controller)
{
	$step = new InstallerStep(array("model"=>"Backend.BackendConfig","fieldGroup"=>"setup",'validateFirst'=> true));
	Installer::addStep($step,"backend_config",6);
}

function BackendBuildAdminMenuHook($backend)
{
	$backend->addMenuItem(ADMIN_SETTINGS, __('Site settings'), array('controller'=>"backend_config",'action'=>"setting",'plugin'=>"backend"));
}

?>